package com.jh3502.vivo.function.message;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jh3502.vivo.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<Map<String, Object>> dataList;

    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    @Nullable
    private ButtonInterface viewClickDelete;
    private boolean isEdit = false;

    public MessageAdapter(Context context, List<Map<String, Object>> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    public void setButtonSetOnclickDelete(ButtonInterface buttonInterface) {
        this.viewClickDelete = buttonInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_message, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        if (isEdit) {
            viewHolder.itemMessageDelete.setVisibility(View.VISIBLE);
        } else {
            viewHolder.itemMessageDelete.setVisibility(View.GONE);
        }
        viewHolder.itemMessageDelete.setOnClickListener(arg0 -> {
            // TODO Auto-generated method stub
            if (viewClickDelete != null) {
                viewClickDelete.onClick(position);
            }
        });

        viewHolder.itemMessageTitle.setText((String) dataList.get(position).get("title"));
        viewHolder.itemMessageCont.setText((String) dataList.get(position).get("content"));

        String num = (String) dataList.get(position).get("num");
        if ("0".equals(num)) {
            viewHolder.itemMessagePoint.setVisibility(View.GONE);
        } else {
            viewHolder.itemMessagePoint.setVisibility(View.VISIBLE);
        }
        viewHolder.itemMessagePoint.setText((String) dataList.get(position).get("num"));
        viewHolder.itemMessageTime.setText((String) dataList.get(position).get("createDate"));

        String type = (String) dataList.get(position).get("type");
        if ("1".equals(type)) {// 出差
            Glide.with(mContext).load(R.drawable.ic_label_travel).into(viewHolder.itemMessageHead);
        } else if ("2".equals(type)) {//报销
            Glide.with(mContext).load(R.drawable.ic_label_reimburse).into(viewHolder.itemMessageHead);
        } else if ("3".equals(type)) {// 系统
            Glide.with(mContext).load(R.drawable.ic_systemmessage).into(viewHolder.itemMessageHead);
        } else if ("4".equals(type)) {// 授权
            Glide.with(mContext).load(R.drawable.ic_information).into(viewHolder.itemMessageHead);
        }

    }

    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }


    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public void setIsEdit(boolean b) {
        isEdit = b;
    }

    public interface ButtonInterface {
        void onClick(int position);
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_message_delete)
        ImageView itemMessageDelete;
        @BindView(R.id.item_message_head)
        ImageView itemMessageHead;
        @BindView(R.id.item_message_title)
        TextView itemMessageTitle;
        @BindView(R.id.item_message_cont)
        TextView itemMessageCont;
        @BindView(R.id.item_message_time)
        TextView itemMessageTime;
        @BindView(R.id.item_message_point)
        TextView itemMessagePoint;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}